﻿using MMS.Model.FormatModel;
using MMS.UI.Extension;
using MMS.UI.Helper;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.WebPages;


namespace MMS.UI.Areas.Admin.Controllers
{
    public class SalesPromotionController : BaseController
    {
        #region 1.2 加载 列表 数据  +Index()
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 1.2 加载 列表 数据 +Index() +HttpPost
        /// <summary>
        /// 1.2 加载 列表 数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            int pageIndex = Request.Form["page"].AsInt();
            int pageSize = Request.Form["rows"].AsInt();
            //1.查询数据集合
            var pageData = OpeCur.GetAsync<PageData<Model.SalesPromotion>>("http://localhost:16824/api/SalesPromotion/GetWherePaged/" + pageIndex + "/" + pageSize);

            //2.转成json格式字符串
            var strJson = OpeCur.ToJson(pageData);
            return Content(strJson);
        }
        #endregion

        #region 2.0 加载 新增 视图 + Add()
        /// <summary>
        /// 2.0 加载 新增 视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        #endregion

        #region 2.1 保存 新增 数据
        /// <summary>
        /// 2.1 保存 新增 数据
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(ViewModels.SalesPromotionModel viewModel)
        {
            if (ModelState.IsValid)
            {
                OpeCur.PostAsJsonAsync<MMS.Model.SalesPromotion>("http://localhost:16824/api/SalesPromotion/PostAdd", viewModel.ToModel());
                return OpeCur.AjaxMsgOK();
            }
            else
            {
                return OpeCur.AjaxMsgNOOK("请不要禁用浏览器js~~!");
            }
        }
        #endregion

        #region 3.0 加载 修改 视图 +Modify(int id)
        /// <summary>
        /// 3.0 加载 修改 视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Modify(int id)
        {
            //1.查询要修改的 权限 实体对象
            var modifyData = OpeCur.GetAsync<MMS.Model.SalesPromotion>("http://localhost:16824/api/SalesPromotion/GetModify/" + id);
            if (modifyData == null) { throw new Exception("找不到要修改的内容~~~~~~~！"); }


            //3.将 实体对象 转成 视图模型对象 传给 视图
            return View(modifyData.ToViewModel());
        }
        #endregion

        #region 3.1 保存 修改 数据
        [HttpPost]
        /// <summary>
        /// 3.1 保存 修改 数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        public ActionResult Modify(int id, ViewModels.SalesPromotionModel viewmodel)
        {
            if (ModelState.IsValid)
            {
                //1.从url参数中 获取 要修改的 对象的 id
                viewmodel.SId = id;
                //2.修改权限
                OpeCur.PostAsJsonAsync<MMS.Model.SalesPromotion>("http://localhost:16824/api/SalesPromotion/PostModify", viewmodel.ToModel());
                return OpeCur.AjaxMsgOK();
            }
            return OpeCur.AjaxMsgNOOK("请不要禁用浏览器js~~~");
        }
        #endregion

        #region 4.0 删除 + Remove()
        /// <summary>
        /// 2.0 删除
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Remove(int id)
        {

            OpeCur.GetApi("http://localhost:16824/api/SalesPromotion/GetRemoveById/" + id);
            return OpeCur.AjaxMsgOK();
        }
        #endregion

        #region 5.0 加载 促销活动响应分析 视图
        /// <summary>
        /// 商品销售趋势分析
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult PromotionAnalyze(string SMinMoney, string SMaxMoney, string SBeginTime, string SEndTime, string SPlace)
        {
            ViewBag.SMinMoney = SMinMoney;
            ViewBag.SMaxMoney = SMaxMoney;
            ViewBag.SBeginTime = SBeginTime;
            ViewBag.SEndTime = SEndTime;
            ViewBag.SPlace = SPlace;
           
            return View();
        }
        #endregion

        #region 5.1 加载 促销活动响应分析 数据
        /// <summary>
        /// 商品销售趋势分析
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PromotionAnalyze(string SMinMoney, string SMaxMoney, string SBeginTime, string SEndTime, string SPlace, FormCollection form)
        {
            int pageIndex = Request.Form["page"].AsInt();
            int pageSize = Request.Form["rows"].AsInt();
            var Data = OpeCur.GetAsync<PageData<Model.MemberInfo>>("http://localhost:16824/api/SalesPromotion/GetWherePagedPromotionAnalyze/" + SMinMoney + "/" + SMaxMoney + "/" + SBeginTime + "/" + SEndTime + "/" + SPlace + "/" + pageIndex + "/" + pageSize);

            var strJson = OpeCur.ToJson(Data);
            return Content(strJson);

        }

        public ActionResult TradeInfoAnalyze(string SMinMoney, string SMaxMoney, string SBeginTime, string SEndTime, string SPlace, FormCollection form)
        {
            int pageIndex = Request.Form["page"].AsInt();
            int pageSize = Request.Form["rows"].AsInt();
            var Data = OpeCur.GetAsync<PageData<Model.TradeInfo>>("http://localhost:16824/api/SalesPromotion/GetWherePagedTradeInfoAnalyze/" + SMinMoney + "/" + SMaxMoney + "/" + SBeginTime + "/" + SEndTime + "/" + SPlace + "/" + pageIndex + "/" + pageSize);

            var strJson = OpeCur.ToJson(Data);
            return Content(strJson);

        }
        #endregion

    }
}
